package com.mobi.Survey.lib.activities;

import java.util.List;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.db.SurveyTable;
import com.mobi.Survey.lib.model.Address;
import com.mobi.Survey.lib.model.SurveyPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyInformation.java This class is responsible for displaying information
 * about survey
 */
public class SurveyInformation extends AbstractScreen {
	private TextView txtCompanyName;
	private TextView txtPhoneNo;

	private TextView txtCntcEmail;
	private TextView txtWebsite;
	private TextView txtSubsBy;
	private TextView txtSubsEmail;

	private long surveyId;
	private SurveyPojo survey = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		hideKeaboard = true;

		Intent intent = getIntent();
		if (intent != null) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				SurveyTable stable = new SurveyTable(this);
				// Survey id of survey for which display information should be
				// greater than 0
				surveyId = ((Long) bundle.get("data")).intValue();
				try {
					survey = stable.getSurveyForInformationScreen(this,
							surveyId);
				} catch (Exception e) {

					showShortToast(Messages.NO_SURVEY_INFO_AVAILABLE);
					finish();
				} finally {
					stable.closeDB();
				}

				setTitle(survey.getSurveyDiscription());
			}

			if (!canCall()) {
				findViewById(R.id.phone).setVisibility(View.GONE);
			}

		}

		txtCompanyName.setText(" " + survey.getCompanyName());

		if (survey.getPhone() != null && survey.getPhone().length() > 0)
			txtPhoneNo.setText(" " + survey.getPhone());

		if (survey.getContactEmail() != null
				&& survey.getContactEmail().length() > 0)
			txtCntcEmail.setText(" " + survey.getContactEmail());

		if (survey.getWebsite() != null && survey.getWebsite().length() > 0)
			txtWebsite.setText(" " + survey.getWebsite());

		txtSubsBy.setText(" " + survey.getSubscription().getfName() + " "
				+ survey.getSubscription().getlName());

		if (survey.getSubscription().getEmail() != null
				&& survey.getSubscription().getEmail().length() > 0)
			txtSubsEmail.setText(" " + survey.getSubscription().getEmail());

		if (survey.getAddress().isAddressNull()) {
			findViewById(R.id.map).setVisibility(View.GONE);
		} else {
			Address address = survey.getAddress();

			String query = ' '
					+ (address.getStreetNumber() == null ? "" : address
							.getStreetNumber() + ", ")
					+ (address.getStreetName() == null ? "" : address
							.getStreetName() + ", ")
					+ (address.getCity() == null ? "" : address.getCity()
							+ ", ")

					+ (address.getState() == null ? "" : address.getState()
							+ ", ")
					+ (address.getCountry() == null ? "" : address.getCountry()
							+ ", ")
					+ (address.getZip() == null ? "" : address.getZip());

			((TextView) findViewById(R.id.tvAddress)).setText(query);
		}

	}

	public void textClick(View view) {

		if (view.getId() == R.id.phone) {
			makeCall(txtPhoneNo.getText().toString());
		} else if (view.getId() == R.id.ContactEmail) {
			sendEmail(txtCntcEmail.getText().toString(),
					survey.getSurveyDiscription(),
					getString(R.string.more_info));
		} else if (view.getId() == R.id.Website) {
			openUrl(txtWebsite.getText().toString());
		} else if (view.getId() == R.id.email) {
			sendEmail(txtSubsEmail.getText().toString(),
					survey.getSurveyDiscription(),
					getString(R.string.more_info));
		} else if (view.getId() == R.id.map) {

			Address address = survey.getAddress();

			openMapByAddress(address.toString());
		}
	}

	@Override
	protected void initUiVars() {
		txtCompanyName = (TextView) findViewById(R.id.tvCompanyName);
		txtPhoneNo = (TextView) findViewById(R.id.tvPhone);

		txtCntcEmail = (TextView) findViewById(R.id.tvContactEmail);
		txtWebsite = (TextView) findViewById(R.id.tvWebsite);
		txtSubsBy = (TextView) findViewById(R.id.tvSubsBy);
		txtSubsEmail = (TextView) findViewById(R.id.tvEmail);

	}

	@Override
	protected int getLayoutId() {

		return LayoutIds.SURVEY_IFNO_SCREEN;
	}

	@Override
	protected void onRelease() {

	}

}
